package com.allinfinance.starters.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;

public abstract class FileUtil {

    /**
     * 读取文件内容
     *
     * @param file 文件
     * @param note 文件行的注释标识
     * @return 文件内容
     * @throws IOException
     */
    public static String readFile(File file, String fileEncoding, String note) throws IOException {

        StringBuilder sb = new StringBuilder();
        try (BufferedReader reader = Files.newBufferedReader(file.toPath(), Charset.forName(fileEncoding))) {
            String line;
            while ((line = reader.readLine()) != null) {
                //跳过空行
                if (line.trim().length() == 0) {
                    continue;
                }
                //跳过注释行
                if (StringUtil.isNotBank(note) && line.trim().startsWith(note)) {
                    continue;
                }
                sb.append(line.trim());
            }
        }
        return sb.toString();
    }

}
